package com.ccsu.joysport.controller;


import com.ccsu.joysport.entity.student;
import com.ccsu.joysport.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/ccsu/admin/setStu")
public class setStudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping
    public List<student> getAllStudents() {
        return studentService.getAllStudents();
    }

    @GetMapping("/{studentNumber}")
    public ResponseEntity<student> getStudentById(@PathVariable String studentNumber) {
        Optional<student> student = studentService.getStudentById(studentNumber);
        return student.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
    }
//    @PostMapping
//    public student createStudent(@RequestBody student student) {
//        return studentService.saveStudent(student);
//    }

    @PutMapping("/{studentNumber}")
    public ResponseEntity<student> updateStudent(@PathVariable String studentNumber, @RequestBody student studentDetails) {
        Optional<student> student = studentService.getStudentById(studentNumber);
        if (student.isPresent()) {
            student updatedStudent = student.get();
            updatedStudent.setName(studentDetails.getName());
            updatedStudent.setCollege(studentDetails.getCollege());
            updatedStudent.setTeam(studentDetails.getTeam());
            updatedStudent.setMajor(studentDetails.getMajor());
            return ResponseEntity.ok(studentService.saveStudent(updatedStudent));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{studentNumber}")
    public ResponseEntity<Void> deleteStudent(@PathVariable String studentNumber) {
        studentService.deleteStudent(studentNumber);
        return ResponseEntity.noContent().build();
    }
    @PostMapping
    public List<student> createStudents(@RequestBody List<student> students) {
        return studentService.saveAllStudents(students);
    }
}
